//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2009-01-01
// Contains ...
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Text;
using System.Xml.Serialization;
using JetBrains.Annotations;
using LargoCommon.Abstract;
namespace LargoCommon.Music
{
/// Harmonic variety.
/// Harmonic variety is subclass of variety. It it designed to keep
/// selected harmonic structures of given Modality.
[Serializable]
[XmlInclude(typeof(HarmonicSystem))]
public class HarmonicStructuralVariety : StructuralVariety {
#region Fields
/// Harmonic functions.
private Dictionary functions;
#endregion
#region Constructors
///
/// Initializes a new instance of the HarmonicStructuralVariety class. Serializable.
///
/// The given system.
public HarmonicStructuralVariety(GeneralSystem givenSystem)
: base(givenSystem) {
this.functions = new Dictionary();
}
#endregion
#region Properties
/// Gets table of harmonic functions.
/// Property description.
[XmlIgnore]
private Dictionary Functions {
get {
Contract.Ensures(Contract.Result>() != null);
if (this.functions == null) {
throw new InvalidOperationException("Functions is null.");
}
return this.functions;
}
}
#endregion
#region Public static methods
/// Initializes a new instance of the HarmonicStructuralVariety class.
/// Abstract modality.
/// Abstract qualifier.
/// Limit for number od structures.
/// Returns value.
[UsedImplicitly]
public static HarmonicStructuralVariety NewHarmonicStructuralVariety(BinaryStructure modality, GeneralQualifier qualifier, int limitCount) {
Contract.Requires(modality != null);
if (modality == null) {
return null;
}
GeneralSystem givenSystem = modality.GSystem;
HarmonicStructuralVariety gsv = new HarmonicStructuralVariety(givenSystem)
{
VarType = StructuralVarietyType.BinarySubstructuresOfModality,
Modality = modality,
Qualifier = qualifier,
LimitCount = limitCount
};
gsv.Generate();
gsv.DetermineFunctions();
gsv.SortStructList(GenProperty.Consonance, GenSortDirection.Descending);
return gsv;
}
#endregion
#region Public methods
/// Returns requested harmonic Function.
/// Harmonic function.
/// Returns value.
public HarmonicStructure Function(HarmonicFunctionType functionItem) {
return this.Functions[functionItem];
}
#endregion
#region String representation
/// String representation of the object.
/// Returns value.
public override string ToString() {
StringBuilder s = new StringBuilder();
s.Append("* H-struct variety *\r\n");
s.Append(base.ToString());
if (this.Modality != null) {
s.Append("Modality:" + this.Modality);
}
s.Append("Functions:" + this.Functions);
return s.ToString();
}
#endregion
#region Harmonic functions
/// Determine harmonic functions.
private void DetermineFunctions() {
if (this.StructList.Count == 0) {
return;
}
this.functions = new Dictionary();
//// this.StructList.ForAll((harmonicStructure) => harmonicStructure.HarmonicModality = (HarmonicModality)Modality);
this.SortStructList(GenProperty.Tonicity, GenSortDirection.Descending); // HarmonicTonicity,FormalPotential
HarmonicStructure tonic = null;
if (this.StructList.Count > 0) {
tonic = this.StructList[0];
this.Functions.Add(HarmonicFunctionType.Tonic, tonic);
}
if (this.StructList.Count > 0) {
HarmonicStructure antitonic = this.StructList[this.StructList.Count - 1];
this.Functions.Add(HarmonicFunctionType.AntiTonic, antitonic);
if (tonic != null) {
this.StructList.ForAll(harmonicStructure => harmonicStructure.DetermineBehaviorToTonic(tonic));
}
}
this.SortStructList(GenProperty.TonicContinuity, GenSortDirection.Descending);
if (this.StructList.Count > 0)
{
HarmonicStructure dominant = this.StructList[0];
this.Functions.Add(HarmonicFunctionType.Dominant, dominant);
}
if (this.StructList.Count > 0)
{
HarmonicStructure subdominant = this.StructList[this.StructList.Count - 1];
this.Functions.Add(HarmonicFunctionType.Subdominant, subdominant);
}
this.SortStructList(GenProperty.TonicImpulse, GenSortDirection.Descending);
if (this.StructList.Count <= 0)
{
return;
}
HarmonicStructure sensitive = this.StructList[0];
this.Functions.Add(HarmonicFunctionType.Sensitive, sensitive);
}
#endregion
}
}